<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\Models\Type;    
use App\Transformers\TypeTransformer;
class TypeController extends Controller
{
    //
    
    public function index(Request $request)
    {
        //城市ID   如果传城市ID 那么就展示城市下的轮播  
          $id = request('city_id','');
            if(!empty($id)){
                   $list =  \DB::table('type')
                           ->where('is_del',0)
                            ->where('city_id',$id)
                            ->get();
                    $list = json_decode(json_encode($list),true);
                    
                    $data = array(
                             'data'=>$list,  
                    );
                    return $data;
            }else{
                
                $list =  \DB::table('type')
                            ->select('type.*','product.id as pid')
                            ->where('type.is_del',0)
                            ->leftJoin('product','product.type_id','=','type.id')
                            ->get();
            
                 $list = json_decode(json_encode($list),true);
                foreach($list as $key=>$v){
                    
                    if(!$v['pid']){
                        unset($list[$key]);
                  }

                   
                }
                  
                    
                    
              
                    
                   return $list;
                
                
          

    }
}
}